package io.sentry.event.interfaces;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import jodd.io.StreamUtil;

/**
 * <br>TODO(描述该类的作用)</br>
 *
 * @author  520878
 * @email   zhaoruding@dafycredit.com
 * @date    2018年4月4日 下午3:08:05
 * @version 1.0
 * @since   1.0
 */
public class BodyReaderWrapper extends HttpServletRequestWrapper{  

	private final byte[] body; //用于保存读取body中数据   

	public BodyReaderWrapper(HttpServletRequest request)     
			throws IOException {    
		super(request);    
		body = StreamUtil.readBytes(request.getReader(), "UTF-8");    
	}    

	@Override    
	public BufferedReader getReader() throws IOException {    
		return new BufferedReader(new InputStreamReader(getInputStream()));    
	}    

	@Override    
	public ServletInputStream getInputStream() throws IOException {    
		final ByteArrayInputStream bais = new ByteArrayInputStream(body);    
		return new ServletInputStream() {    
			@Override    
			public int read() throws IOException {    
				return bais.read();    
			}  
		};    
	}    
}
